package jk.fox.engine.context;

import jk.fox.annotaion.RetryState;
import jk.fox.annotaion.RpcReference;
import jk.fox.annotaion.RpcService;
import jk.fox.engine.RpcNodeDefinition;

/**
 * Rpc 注解解析器
 * @author cuichao
 * @Description: RpcAnnotationResolver
 * @Date: create in 2020/6/2 10:40
 */
public class RpcAnnotationResolver {


    public void resolveServiceAnnotation(RpcService rpcService, RpcNodeDefinition rdf){
        if(!"".equals(rpcService.id())){
            rdf.setId(rpcService.id());
        }
        if(rpcService.service() != Void.class && rdf.getTarget() == null){
            rdf.setTarget(rpcService.service());
        }

        if(rdf.getEnableRetry() != null || rpcService.retry() == RetryState.Empty ){
            return;
        }

        if(rpcService.retry() == RetryState.False){
            rdf.setEnableRetry(false);
            return;
        }
        rdf.setEnableRetry(true);

        if(rpcService.retires() > 0 && rdf.getMaxRetires() == null){
            rdf.setMaxRetires(rpcService.retires());
        }
        if(rpcService.timeout() > 0 && rdf.getTimeoutMS() == null){
            rdf.setTimeoutMS(rpcService.timeout());
        }

    }


    public void resolveReferenceAnnotation(RpcReference rpcReference, RpcNodeDefinition rdf){
        if(!"".equals(rpcReference.id())){
            rdf.setId(rpcReference.id());
        }
        if( rpcReference.retry() == RetryState.Empty ){
            return;
        }
        if(rpcReference.retry() == RetryState.False){
            rdf.setEnableRetry(false);
            return;
        }
        rdf.setEnableRetry(true);
        if(rpcReference.retires() > 0){
            rdf.setMaxRetires(rpcReference.retires());
        }
        if(rpcReference.timeout() > 0){
            rdf.setTimeoutMS(rpcReference.timeout());
        }
    }

}
